﻿using Cms.Core.Application.Service.UserInfo.Dto;
using Cms.Core.Validation.Abstractions;
using FluentValidation;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading.Tasks;

namespace Cms.Core.Validation.Validation
{
   public class UserDtoValidation : BaseAbstractValidator<UserDto>
    {

        public UserDtoValidation()
        {
            RuleFor(x => x.UserName).NotEmpty().When(o => o.UserName != null).WithMessage("用户名不能为空!!");
            RuleFor(x => x.AccountNumber).NotEmpty().When(o => o.AccountNumber != null).WithMessage("登录名不能为空!!");
            RuleFor(x => x.RoleIds).MustAsync(async(o, cancellation)=> {
                await Task.CompletedTask;
                return o.Count() >= 1;
            }).WithMessage("请选择角色!!"); 
            //RuleFor(x => x.RoleIds).NotNull().When(o => o.RoleIds.Count() <= 0).WithMessage("请选择角色!!");
            //RuleFor(x => x.RoleIds).NotNull().WhenAsync(async (o, cancellation) =>
            //{
            //    await Task.CompletedTask;
            //    return o.RoleIds.Count() >= 1 ? true : false;

            //}).WithMessage("请选择角色!!");

        }
    }
}
